﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using VkcomAPI;
using VkcomAPI.Core;
using VkcomAPI.PublicTypes;

namespace AudioGrabber
{
    public partial class MainWindow : Window
    {
        private ApiMain VkAPI
        {
            get;
            set;
        }

        public TokenAndID tokenAndId = null;

        public MainWindow()
        {
            InitializeComponent();
            VkAPI = new ApiMain();
        }

        private void authorizeButton_Click(object sender, RoutedEventArgs e)
        {
                LoginDialog loginDialog = new LoginDialog(VkAPI.Authorize);
                SearchGrid.IsEnabled = loginDialog.Authorize();
        }

        private void ButtonSearch_Click(object sender, RoutedEventArgs e)
        {
            songsBox.ItemsSource = VkAPI.SearchAudio(searchSongBox.Text);
        }

        private void searchSongBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            ButtonSearch.IsEnabled = searchSongBox.Text.Length > 0;
        }

        private void downloadButton_Click(object sender, RoutedEventArgs e)
        {
            SongInfo selectedSong = (SongInfo)songsBox.SelectedItem;
            if (selectedSong == null)
                return;
            DownloadSingleFile(selectedSong);
        }

        private void DownloadSingleFile(SongInfo selectedSong)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = ".mp3";
            saveDialog.Filter = "MP3 Files (.mp3)|*.mp3";
            saveDialog.FileName = selectedSong.Artist + " - " + selectedSong.Track;
            if (saveDialog.ShowDialog() == true)
            {
                pbDownloadProgress.Visibility = Visibility.Visible;
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                    wc.DownloadFileAsync(new Uri((selectedSong).Url), saveDialog.FileName);
                }
            }
        }

        private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            pbDownloadProgress.Value = e.ProgressPercentage;
            if (e.ProgressPercentage == 100)
                pbDownloadProgress.Visibility = Visibility.Collapsed;
        }

        private void songsBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            downloadButton.IsEnabled = ((ListBox)sender).SelectedIndex != -1;
        }

        private void downloadAllAudiosButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = ".mp3";
            saveDialog.Filter = "MP3 Files (.mp3)|*.mp3";
            SongInfo selectedSong = (SongInfo)songsBox.SelectedItem;

            if (saveDialog.ShowDialog() == true)
                using (WebClient wc = new WebClient())
                    wc.DownloadFileAsync(new Uri((selectedSong).Url), saveDialog.FileName);
        }
    }
}